package com.atguigu.flink.chapter06;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.ProcessingTimeSessionWindows;
import org.apache.flink.streaming.api.windowing.assigners.SessionWindowTimeGapExtractor;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

public class WindowDemo3 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        env.socketTextStream("hadoop102",8888)
                .flatMap(new FlatMapFunction<String, Tuple2<String,Integer>>() {
                    @Override
                    public void flatMap(String value,
                                        Collector<Tuple2<String, Integer>> out) throws Exception {
                        String[] words = value.split(" ");
                        for (String word : words) {
                            out.collect(Tuple2.of(word,1));
                        }
                    }
                })
                .keyBy(t -> t.f0)
                //定义窗口：session窗口
                //.window(ProcessingTimeSessionWindows.withGap(Time.seconds(3)))
                .window(ProcessingTimeSessionWindows.withDynamicGap(new SessionWindowTimeGapExtractor<Tuple2<String, Integer>>() {
                    @Override
                    public long extract(Tuple2<String, Integer> t) {
                        return new Random().nextInt(10) * 1000 + 1000;
                    }
                }))
                //窗口处理函数： 如何处理窗口中的元素
                /*
                参数1 : 输入
                参数2 ：输出
                参数3 ：Key的类型
                参数4 ： 窗口的类型
                 */
                .process(new ProcessWindowFunction<Tuple2<String, Integer>, String, String, TimeWindow>() {
                    @Override
                    public void process(String key,  //窗口属于的key
                                        Context ctx,  //上下文对象： 获取窗口的开始和结束
                                        Iterable<Tuple2<String, Integer>> elements,   //集合：存储当前窗口的所有元素
                                        Collector<String> out) throws Exception {
                        ArrayList<String> list = new ArrayList<>();
                        for (Tuple2<String, Integer> t : elements) {
                            list.add(t.f0 + "_" + t.f1);
                        }
                        String msg = new Date(ctx.window().getStart()) + "," + new Date(ctx.window().getEnd()) + "," + list;
                        out.collect(msg);
                    }
                })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
